//
//  tipsViewController.m
//  INAlienLand
//
//  Created by dllo on 15/7/9.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "tipsViewController.h"
#import "HTTPTool.h"
#import "tipsModel.h"
#import "tipTableViewCell.h"
#import "tripsModel.h"
#import "UIImageView+WebCache.h"
#import "tripsTableViewCell.h"



#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface tipsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UITableView *tipTableView;
@property (nonatomic,retain)UITableView *noteTableView;
@property (nonatomic,retain)NSMutableArray *allTipArr;
@property (nonatomic,retain)NSMutableArray *allTripsArr;


@end

@implementation tipsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allTipArr = [NSMutableArray array];
    self.allTripsArr = [NSMutableArray array];
    // Do any additional setup after loading the view.
    [self createSeg];
    [self createTiptable];
    [self createNoteTable];
    [self createData];
    
}
-(void)createData
{
    
    [HTTPTool get:@"http://api.breadtrip.com/destination/place/5/2388365275/tips/?start=0&tip_id=" body:nil HTTPResult:JSON success:^(id result) {
        NSDictionary *Dic = result;
        NSArray *tipsArr=Dic[@"tips"];
        for (NSDictionary *tempDic in tipsArr) {
            tipsModel *tip = [[tipsModel alloc]init];
            [tip setValuesForKeysWithDictionary:tempDic];
            [self.allTipArr addObject:tip];
        }
        
        [self.tipTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    [HTTPTool get:@"http://api.breadtrip.com/destination/place/5/2388365275/poi_trips/?start=0&count=5" body:nil HTTPResult:JSON success:^(id result) {
        NSDictionary *Dic = result;
        NSArray *tipsArr=Dic[@"trips"];
        for (NSDictionary *tempDic in tipsArr) {
            tripsModel *tip = [[tripsModel alloc]init];
            [tip setValuesForKeysWithDictionary:tempDic];
            [self.allTripsArr addObject:tip];
        }
        NSLog(@"111");
        [self.noteTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    
}
-(void)createTiptable
{
    self.tipTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.tipTableView];
    self.tipTableView.delegate = self;
    self.tipTableView.dataSource = self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.tipTableView]) {
        return self.allTipArr.count;
    }else
    {
        return self.allTripsArr.count;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.tipTableView]) {
        static NSString *reuse = @"reuse";
        tipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[tipTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        tipsModel *tip = self.allTipArr[indexPath.row];
        cell.labeltime.text = tip.datetime;
        
        //cell.labelrate.text =tip.rating;
        cell.labelcontent.text = [NSString stringWithFormat:@"%@%@%@",@"",@"2",tip.content];
        cell.labelname.text =tip.user[@"name"] ;
        
        NSString *lev =[NSString stringWithFormat:@"%@",tip.user[@"experience"][@"level_info"][@"value"]];
        cell.labellev.text=lev;
        
        NSString *str = tip.user[@"avatar_s"];
        [cell.touxiang sd_setImageWithURL:[NSURL URLWithString:str]];
        
        return cell;
    }else
    {
        static NSString *tripreuse = @"tripreuse";
        tripsTableViewCell *tripcell = [tableView dequeueReusableCellWithIdentifier:tripreuse];
        if (!tripcell) {
            tripcell = [[tripsTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:tripreuse];
        }
        tripsModel *trips =self.allTripsArr[indexPath.row];
        tripcell.labelContent.text =trips.trip_text;
        return tripcell;
    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.tipTableView]) {
        return 100;
    }else
    {
        return 200;
    }
}
-(void)createNoteTable
{
    self.noteTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.noteTableView ];
    self.noteTableView.delegate = self;
    self.noteTableView.dataSource = self;
    
}
-(void)createSeg
{
    UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"游记",@"点评"]];
    seg.frame = CGRectMake(0, 0, self.view.frame.size.width/2, 30);
    seg.layer.cornerRadius =15;
    seg.layer.borderWidth = 1;
    seg.layer.masksToBounds = YES;
    self.navigationItem.titleView=seg;
    [seg addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
}
-(void)click:(UISegmentedControl *)seg
{
    if (seg.selectedSegmentIndex == 0) {
        [self.view bringSubviewToFront:self.noteTableView];
    }
    else  if (seg.selectedSegmentIndex == 1) {
        [self.view bringSubviewToFront:self.tipTableView];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
